import logging
import os

import toml

BASE_DIR = os.path.dirname(os.path.abspath(__file__))


def get_config_info():
    config_file = os.path.join(BASE_DIR, "../config.toml")
    with open(config_file, "r", encoding="utf-8") as f:
        config_info = toml.load(f)
    return config_info


def get_specified_config_info(key):
    config_info = get_config_info()
    return config_info.get(key, None)


def get_value_from_config(key):
    config_info = get_specified_config_info(key)
    if config_info is None:
        logging.warning(f'配置信息中，没有找到{key}, 将返回空字符串')
        return ''
    return config_info


def get_list_from_config(key):
    config_info = get_specified_config_info(key)
    if config_info is None:
        logging.warning(f'配置信息中，没有找到{key}, 将返回空列表')
        return []
    return config_info


def get_dict_from_config(key):
    config_info = get_specified_config_info(key)
    if config_info is None:
        logging.warning(f'配置信息中，没有找到{key}, 将返回空字典')
        return {}
    return config_info


def get_access_token_from_config():
    return get_value_from_config('gitee_token')


def get_file_check_list_from_config():
    return get_list_from_config('file_to_check')


def get_repo_setting_dict_from_config():
    return get_dict_from_config('repo_setting')


def get_repos_no_setting_from_config():
    return get_list_from_config('repo_no_need_to_change_setting')


if __name__ == '__main__':
    print(get_value_from_config('test'))
